<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `ilovechina,thewordisbeautiful
        i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful`
        let spl = input.split('\n')
        let list = spl.shift().split(',')
        let dictionary = spl[0].split(',').map(v => v.trim())


        let res=[]

        function findfoo(item){
            let  str=''
            let max_str_len=0
            let max_str=''
            let  dic_now=[]

            let [l,r]=[0,1]

            while(r<=item.length){
                str=item.slice(l,r)
                dictionary.forEach((v)=>{
                    if(v.length>=str.length){
                        if(str===v.slice(0,r-l)){
                            dic_now.push(v)
                        }
                    }
                })

                // console.log(dic_now,str,max_str,l,r,res);
                

                if(dic_now.length){
                    dic_now.forEach((v)=>{
                        if(str===v){
                            if(str.length>=max_str_len){
                                max_str=str
                                max_str_len=max_str.length                              
                            }
                        }
                    })
                    dic_now=[]
                    r++
                    if(r>item.length){
                        res.push(str)
                    }

                }else{
                    if(max_str_len){
                        dic_now=[]
                        res.push(max_str)
                        l+=max_str_len
                        r=l+1
                        max_str=''
                        max_str_len=0
                    }else{
                        dic_now=[]
                        res.push(str[0])
                        max_str=''
                        max_str_len=0
                        l++
                        r=l+1
                    }
                }


            }
        }
        for(let item of list){
            findfoo(item)
        }

     

        console.log(res);
        


        console.log(list,dictionary);
        


        



    </script>
</body>

</html>